package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.BoughtGoods;
import com.wholesale.beans.ShippingMethod;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.ShippingMethodListAdapter;

public class OrderShippingMethodLayout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;

	private ShippingMethod currentShippingMethod;
	private ShippingMethodListAdapter listAdapter;
	private CategoriesListAsync categoriesAsync;
	private int addressId;
	private ArrayList<BoughtGoods> boughtGoodsList;

	// 构造函数
	public OrderShippingMethodLayout(Context context,
			final ViewFlipper viewFlipper,
			ShippingMethod currentShippingMethod, int addressId,int selectIndex,
			ArrayList<BoughtGoods> boughtGoodsList) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.currentShippingMethod = currentShippingMethod;
		this.addressId = addressId;
		this.boughtGoodsList =boughtGoodsList ;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.ordershipmethod, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.GONE);
		rightButton.setText("OK");
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Shipping Method");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);

		bodyList.setSelector(R.drawable.categories_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new ShippingMethodListAdapter(context, bodyList);
		listAdapter.setSelected(selectIndex);
		bodyList.setAdapter(listAdapter);

		categoriesAsync = new CategoriesListAsync(context);
		categoriesAsync.execute(addressId);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			currentShippingMethod = listAdapter.getItem(arg2);
//			listAdapter.setSelected(arg2);
//			listAdapter.notifyDataSetChanged();
//			bodyList.postInvalidate();
			int count = viewFlipper.getChildCount();
			View preView = viewFlipper.getChildAt(count - 2);
			if (preView.getClass().getName().equals(
					CheckOutLayout.class.getName())) {
				((CheckOutLayout) preView)
						.setShippingMethod(arg2,currentShippingMethod);
			}
			viewFlipper.showPrevious();
			viewFlipper.removeView(OrderShippingMethodLayout.this);
		}
	};

	class CategoriesListAsync extends
			AsyncTask<Integer, Integer, ArrayList<ShippingMethod>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public CategoriesListAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<ShippingMethod> doInBackground(Integer... params) {
			return WebServiceClient.getShippingInfoByAddress(params[0],boughtGoodsList);
		}

		@Override
		protected void onPostExecute(ArrayList<ShippingMethod> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			if (currentShippingMethod != null) {
				int size = result.size();
				int curIndex = 0;
				for(int i=0;i<size;i++){
					if(result.get(i).getShippingId()== currentShippingMethod.getShippingId()){
						curIndex = i ;
						break ;
					}
				}
				listAdapter.setSelected(curIndex);
			} else if (result.size() > 0) {
				currentShippingMethod = result.get(0);
			}
			listAdapter.notifyDataSetChanged();
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			if(currentShippingMethod != null){
//				int count = viewFlipper.getChildCount();
//				View preView = viewFlipper.getChildAt(count - 2);
//				if (preView.getClass().getName().equals(
//						CheckOutLayout.class.getName())) {
//					((CheckOutLayout) preView)
//							.setShippingMethod(currentShippingMethod);
//				}
//				viewFlipper.showPrevious();
//				viewFlipper.removeView(this);
			}
			break;
		default:
			break;
		}

	}
}
